<template>
  <div>
    <div>
      <ele-page
        v-loading="loading"
        :formConfig="searchDialog"
        ref="eleTable"
        :column="column"
        :operateList="operateList"
        @handleOperateEvent="handleOperateEvent"
        :getList="mesTmMaterialBasePage"
        uniqueId="mesTmMaterialBaseTable"
      ></ele-page>
    </div>
    <ele-form-drawer class="dialog-pack"
                     :isLoading="isLoading"
                     v-model="formDataDialog"
                     v-bind="formDescDialog"
                     :request-fn="handleSubmit"
                     :visible.sync="visible"
                     :title="title"
    >
    </ele-form-drawer>
    <!-- 此组件放到与抽屉平级，理论上不需要修改 -->
  </div>
</template>

<script>
import { operateList, searchConfig, formDescDialogMixin, rowConfig } from './config'


import {mesTmMaterialBaseCreate, mesTmMaterialBaseDeleteById, mesTmMaterialBasePage, mesTmMaterialBaseUpdate} from "@/api/materials/tmMaterialBase";
import {getDict} from "@/api/common";

export default {
  name: 'mesTmMaterialBase',
  mixins: [ formDescDialogMixin, searchConfig],
  data() {
    const self = this
    return {
      title: null,
      visible: false,
      isLoading: false,
      loading: false,

      formDataDialog: {},
      // formDescDialog, //新增修改框
      operateList,
      mesTmMaterialBasePage,
      // 列表页字段的绑定配置
      column: [
        {
          type: 'selection',
          fixed: 'left'
        },
        {
          type: 'index',
          label: $i18n.t('common.order'),
          width: 60,
          fixed: 'left'
        },
        {
          prop: "materialType",
          label: '类型',
          optionProp: {
            value: "value",
            label: "description",
          },
          option: async (data) => {
            const list = await getDict("tm_material_base")
            return list.data.result;
          },
          showOverflowTooltip: true,
        },
        {
          prop: 'materialCode',
          label: '物料编号',
          showOverflowTooltip: true
        },
        {
          prop: 'materialName',
          label: '物料名称',
          showOverflowTooltip: true
        },
        {
          prop: 'operate',
          label: $i18n.t('common.operation'),
          width: 160,
          fixed: 'right',
          render: (h, scope) => {
            return <ele-row-operation scope={scope} rowConfig={rowConfig} onRowOperate={this.rowOperate} />
          }
        }
      ]
    }
  },
  computed: {},
  methods: {
    // 表格顶部按钮操作事件
    handleOperateEvent(data) {
      this.title = data.name
      const fnc = {
        add: () => {
          this.visible = true
        },
        deleteBatch: () => {
          this.$confirm('此操作将删除所选数据, 是否继续?', $i18n.t('common.tip'), {
            confirmButtonText: $i18n.t('common.ok'),
            cancelButtonText: $i18n.t('common.cancel'),
            type: 'warning'
          }).then(() => {
            const ids = data.rows.map((val) => val.id).join(',')
            this.rowDel(ids)
          })
        },
      }
      fnc[data.handleName]()
    },
    // 表格行操作事件
    rowOperate({ handleName, name, row }) {
      this.title = name
      const fnc = {
        edit: () => {
          this.formDataDialog = { ...row }
          this.visible = true
        },
        delete: () => {
          this.$confirm('此操作将删除所选数据, 是否继续?', $i18n.t('common.tip'), {
            confirmButtonText: $i18n.t('common.ok'),
            cancelButtonText: $i18n.t('common.cancel'),
            type: 'warning'
          }).then(() => {
            this.rowDel(row.id)
          })
        }
      }
      fnc[handleName]()
    },
    doSubmit({ title, api, data }) {
      this.isLoading = true
      api(data)
        .then((res) => {
          const message = res.data.message
          this.$message.success(message || title)
          this.visible = false
          this.refresh()
        })
        .finally(() => {
          this.isLoading = false
        })
    },
    // 弹框提交事件
    handleSubmit(data) {
      if (this.isLoading) return
      const params = this.formDataDialog.id
        ? {
          title: $i18n.t('common.editSuccess'),
          api: mesTmMaterialBaseUpdate,
          data
        }
        : {
          title: $i18n.t('common.addSuccess'),
          api: mesTmMaterialBaseCreate,
          data
        }
      console.log(params)
      this.doSubmit(params)
    },
    // 删除
    rowDel(data) {
      this.loading = true
      mesTmMaterialBaseDeleteById(data)
        .then((res) => {
          const message = res.data.message
          this.$message.success(message || $i18n.t('common.deleteSuccess'))
          this.refresh()
        })
        .finally(() => {
          this.loading = false
        })
    },
    // 刷新表格数据
    refresh(isReset = true) {
      this.$refs.eleTable.refresh(isReset)
    }
  }
}
</script>
